package com.example.lyqwork.service;

import com.example.lyqwork.pojo.CarOrder;
import com.example.lyqwork.pojo.dto.CarOrderDto;
import com.example.lyqwork.pojo.dto.UserEditDto;
import com.example.lyqwork.repository.CarOrderRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderService implements IOrderService {

    @Autowired
    CarOrderRepository carOrderRepository;



    @Override
    public CarOrder add(CarOrderDto order) {
        //
        System.out.println(order.toString());
        CarOrder tempUser = new CarOrder(order.getCustomerName(), order.getPlanName(),  order.getUserId());

        // 确保 UserDto 和 User 类的属性一致
        System.out.println(tempUser.toString());
        return carOrderRepository.save(tempUser);
    }

    @Override
    public List<CarOrder> getOrder(Integer userId,Integer page,Integer pageSize) {
        // 查询最近三条订单，按 orderId 降序排列
        return carOrderRepository.findByUserIdOrderByOrderIdDesc(userId, PageRequest.of(page, pageSize));
    }

    @Override
    public List<CarOrder> getOrderByOrderId(Integer orderId) {
        // 查询最近三条订单，按 orderId 降序排列
        return carOrderRepository.findByOrderIdOrderByOrderIdDesc(orderId, PageRequest.of(0, 1));
    }


    @Override
    public CarOrder update(UserEditDto user) {
        return null;
    }

    @Override
    public void deleted (Integer orderId) {
        carOrderRepository.deleteById(orderId);
    }

    @Override
    public CarOrder update(CarOrder order) {
        return carOrderRepository.save(order);
    }

}
